package com.chinaway.mobile.balance.impl;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.chinaway.mobile.authcode.AuthCodeDiscern;
import com.chinaway.mobile.balance.MobileBalance;
import com.chinaway.mobile.http.util.ClientOperate;
import com.chinaway.mobile.model.Account;
import com.chinaway.proxy.Proxy;
import com.chinaway.proxy.ProxyService;
public class MobileBalanceZjImpl{}
//public class MobileBalanceZjImpl implements MobileBalance {
//	private final Log log = LogFactory.getLog(getClass());
//
//	// Don't change the following URL
//	private static String zheJiangLoginURL = "http://sso.zj.chinamobile.com/Login";
//
//	private static String randCodeURL = "http://sso.zj.chinamobile.com/ImgDisp?temp="
//			+ System.currentTimeMillis();
//
//	// 登陆完成后,各种跳转
//	private static String artiForm = "http://ssoproxy.zj.chinamobile.com/ssoproxy/proxy.do?style=1&rfurl=http%3A%2F%2Fwww.zj.10086.cn%2Fsns";
//
//	// 查询余额
//	private static String balanceURL = "http://www.zj.10086.cn/sns/account.php";
//
//	private HttpResponse response;
//	private HttpEntity entity;
//
//	private DefaultHttpClient httpclient = null;
//
//	private CookieStore cookieStore = null;
//
//	private HttpContext localContext = null;
//	private Proxy proxy;
//
//	public void setProxy(Proxy proxy) {
//		this.proxy = proxy;
//	}
//
//	private ClientOperate co = new ClientOperate();
//	private AuthCodeDiscern authCodeDiscern;
//
//	@Override
//	public Account account(String mobile, String password, String date, Proxy px) {
//		httpclient = new DefaultHttpClient();
//		cookieStore = new BasicCookieStore();
//		localContext = new BasicHttpContext();
//		HttpClientParams.setCookiePolicy(httpclient.getParams(),
//				CookiePolicy.BROWSER_COMPATIBILITY);
//		localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//		httpclient.getParams().setParameter(
//				"http.protocol.single-cookie-header", true);
//		Account account = login(mobile, password);
//		if (account == null)
//			return null;
//		if (account.getCode() == 0) {
//			String accountText = co.getText(httpclient, localContext,
//					balanceURL);
//			Document doc = Jsoup.parse(accountText);
//			Elements elements = doc.getElementsByClass("p12");
//			String text = elements.get(1).text();
//			String balance = StringUtils
//					.substringBetween(text, "实际可用余额：", "元，");
//			String expires = StringUtils.substringBetween(text, "有效期至:", "日");
//			account.setBalance(balance);
//			account.setExpires(expires);
//			return account;
//		}
//		httpclient.getConnectionManager().shutdown();
//		loginCount = 0;
//		return account;
//	}
//
//	private int loginCount = 0;
//
//	private Account login(String mobile, String password) {
//		Account account = new Account(mobile, password);
//		loginCount++;
//		if (loginCount > 3) {
//			loginCount = 0;
//			account.setCode(3333);
//			account.setMessage("登陆3次未成功,请手动处理");
//			return account;
//		}
//		String randCode = getRandCode();
//		HttpPost httpost = new HttpPost(zheJiangLoginURL);
//		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//		nvps.add(new BasicNameValuePair("domainId", "21"));
//		nvps.add(new BasicNameValuePair(
//				"succUrl",
//				"http://ssoproxy.zj.chinamobile.com/ssoproxy/proxy.do?style=1&rfurl=http%3A%2F%2Fwww.zj.10086.cn%2Fsns"));
//		nvps.add(new BasicNameValuePair("errorUrl",
//				"http://www.zj.10086.cn/sns?err="));
//		nvps.add(new BasicNameValuePair("billId", mobile));
//		nvps.add(new BasicNameValuePair("passwd", password));
//		nvps.add(new BasicNameValuePair("validateKey", randCode));
//		try {
//			httpost.setEntity(new UrlEncodedFormEntity(nvps, "GBK"));
//			httpclient.getParams().setParameter("http.protocol.cookie-policy",
//					CookiePolicy.BROWSER_COMPATIBILITY);
//			response = httpclient.execute(httpost, localContext);
//			entity = response.getEntity();
//			String contentText = EntityUtils.toString(entity, "GBK");
//			String content = StringUtils.substringBetween(contentText,
//					"encodeURIComponent('", "')");
//			String aiisoUrl = "http://sso.zj.chinamobile.com/AISSOArtifact?referer=";
//			if (StringUtils.isBlank(content)) {
//				log.debug(contentText);
//				content = StringUtils.substringBetween(contentText, "err=",
//						"\">");
//				if (content.equals("8")) {
//					if (loginCount > 3) {
//						account.setCode(2);
//						account.setMessage("验证码错误");
//						return account;
//					}
//					login(mobile, password);
//				} else if (content.equals("9")) {
//					account.setCode(9);
//					account.setMessage("手机卡被锁定");
//				} else if (content.equals("7") || content.equals("1")
//						|| content.equals("10") || content.equals("19999")
//						|| content.equals("13000")) {
//					account.setCode(1);
//					account.setMessage("用户名或密码错误");
//					log.info("用户名或密码错误");
//				}
//
//				return account;
//			}
//
//			aiisoUrl = aiisoUrl + content;
//			String aiisoContent = co
//					.getText(httpclient, localContext, aiisoUrl);
//
//			String AI_ARTIFACT_Value = StringUtils.substringBetween(
//					aiisoContent, "value=\"", "\">");
//
//			httpost = new HttpPost(artiForm);
//			nvps.clear();
//			nvps.add(new BasicNameValuePair("AI_ARTIFACT", AI_ARTIFACT_Value));
//			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
//			response = httpclient.execute(httpost, localContext);
//			entity = response.getEntity();
//			String locationText = EntityUtils.toString(entity, "GBK");
//			String locationURL = StringUtils.substringBetween(locationText,
//					"<a href=\"", "\">");
//			if (StringUtils.isNotBlank(locationURL)) {
//				co.getText(httpclient, localContext, locationURL);
//			}
//		} catch (Exception e) {
//			account.setCode(1);
//			account.setMessage("用户名或密码错误");
//		} finally {
//			httpost.abort();
//		}
//		loginCount = 0;
//		return account;
//	}
//
//	int i = 0;
//
//	private String getRandCode() {
//		i++;
//		if (i > 5) {
//			i = 0;
//			return "";
//		}
//		// 验证码
//		String randCode = "";
//		try {
//			randCode = co.getResponse(httpclient, localContext, randCodeURL,
//					new ClientOperate.RespProcess() {
//						public String doProcess(HttpResponse response) {
//							String rc = "";
//							entity = response.getEntity();
//							if (entity != null) {
//								try {
//									InputStream instream = entity.getContent();
//									BufferedImage image = ImageIO
//											.read(instream);
//									rc = authCodeDiscern.discern(image, false);
//
//								} catch (Exception e) {
//									e.printStackTrace();
//								}
//							}
//							return rc;
//						}
//					});
//		} catch (ClientProtocolException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		if (StringUtils.isNotBlank(randCode)) {
//			i = 0;
//			return randCode;
//		} else {
//			getRandCode();
//		}
//		i = 0;
//		return "";
//	}
//
//	@Override
//	public boolean check(String city) {
//		return "zj".equals(city);
//	}
//
//	public void setAuthCodeDiscern(AuthCodeDiscern authCodeDiscern) {
//		this.authCodeDiscern = authCodeDiscern;
//	}
//
//}
